package com.sxz.sbs.module.client.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sxz.sbs.R;
import com.sxz.sbs.module.client.bean.ClientListBean;
import com.willy.ratingbar.ScaleRatingBar;

import java.util.List;

/**
 * 客户适配器（所有客户，意向客户，已交定金客户，已成交客户）
 *
 * @author hp
 */

public class ClientAdapter extends BaseQuickAdapter<ClientListBean.ListBean, ClientAdapter.ViewHolder> {

    private OnClientClickListener onClientClickListener;

    public void setOnClientClickListener(OnClientClickListener onClientClickListener) {
        this.onClientClickListener = onClientClickListener;
    }

    public ClientAdapter(@Nullable List<ClientListBean.ListBean> data) {
        super(R.layout.item_client, data);

    }

    @Override
    protected void convert(ViewHolder helper, final ClientListBean.ListBean item) {
        helper.tvState.setText(item.getDisplayStatus());
        helper.setText(R.id.tv_clientname, item.getTxt_UserName());
        String phone = "";
        if (!TextUtils.isEmpty(item.getTxt_Phone())) {
            String splitVal = "\\,";
            if (item.getTxt_Phone().split(splitVal).length > 0) {
                phone = item.getTxt_Phone().split(",")[0];
            }
        }
        helper.setText(R.id.tv_phone_address, phone + "    " + ((item.getAddr_Address() == null) ? "" : item.getAddr_Address()));
        if (item.getCallCount() == 0) {
            helper.setText(R.id.tv_callnum, "已经沟通0次   立即联系让他成为你的金主吧");
        } else {
            helper.setText(R.id.tv_callnum, "已经沟通" + item.getCallCount() + "次    " + item.getCallTime());
        }

        helper.setText(R.id.tv_newschedule, "最新进度：" + (TextUtils.isEmpty(item.getTrackContent()) ? "暂无记录" : item.getTrackContent()));
        if (item.getValStartLevel() == 0) {
            helper.scaleRatingBar.setVisibility(View.GONE);
        } else {
            helper.scaleRatingBar.setVisibility(View.VISIBLE);
            helper.scaleRatingBar.setNumStars(item.getValStartLevel());
            helper.scaleRatingBar.setRating(item.getValStartLevel());
        }
        helper.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClientClickListener != null) {
                    onClientClickListener.onItemCallPhone(v, item);
                }
            }
        });
        helper.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClientClickListener != null) {
                    onClientClickListener.onItemEditSchedule(v, item);
                }
            }
        });

    }

    class ViewHolder extends BaseViewHolder {
        ScaleRatingBar scaleRatingBar;
        TextView tvState;
        ImageView call;
        ImageView edit;

        public ViewHolder(View view) {

            super(view);
            scaleRatingBar = view.findViewById(R.id.simpleRatingBar);
            tvState = view.findViewById(R.id.tv_state);
            call = view.findViewById(R.id.iv_call);
            edit = view.findViewById(R.id.img_itemrecord);
        }
    }

    public interface OnClientClickListener {
        /**
         * 电话号码点击监听
         *
         * @param view
         * @param item
         */
        void onItemCallPhone(View view, ClientListBean.ListBean item);

        /**
         * 修改日程点击监听
         *
         * @param view
         * @param item
         */
        void onItemEditSchedule(View view, ClientListBean.ListBean item);
    }
}
